package com.sinodata.bsm.cicp.finder.hp;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * Description: HP主机发现程序。
 * 程序返回：主机名，OS类型，主机型号，OS版本
 * 
 */
public class OS extends HPFinder {
    private static final Logger logger = Logger.getLogger(OS.class);
    private static final String SHELL = "uname -a";

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] osRes = null;
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            String hostName = null;
            String osType = null;
            String hostModel = null;
            String osEdition = null;
            while ((strLine = reader.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(strLine);
                if (st.countTokens() >= 4) {
                    osRes = new String[7];
                    list.add(osRes);
                    osRes[0] = "0";
                    osRes[1] = "OS0";
                    osRes[2] = getHostName(ip) + "OS0";
                    osType = st.nextToken();//OS类型
                    hostName = st.nextToken();//主机名
                    osEdition = st.nextToken();//OS版本
                    hostModel = st.nextToken();//主机型号
                    osRes[3] = hostName;
                    osRes[4] = osType;
                    osRes[5] = hostModel;
                    osRes[6] = osEdition;
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find HP OS", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failded to close reader", e);
                }
            }
        }
        return null;
    }
}
